/*
 * ShowImg.java
 *
 * Created on 13 październik 2007, 16:10
 */

package info.mikrotubula.utils;

import info.mikrotubula.agents.FileUploadAgent;
import info.mikrotubula.managers.UsersManagerLocal;
import info.mikrotubula.managers.WorkManagerLocal;
import java.io.*;
import java.net.*;
import javax.ejb.EJB;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Krystian
 * @version
 */
public class ShowImg extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
    String contentType = (String)request.getAttribute("fileupload_type");
    String fileName = (String)request.getAttribute("fileupload_name");
    FileUploadAgent agent = (FileUploadAgent) request.getSession().getAttribute("FileUploadAgent");
    String allowCache = request.getParameter("allowCache");
    String openDirectly = request.getParameter("openDirectly");

    if(allowCache == null || allowCache.equalsIgnoreCase("false"))
    {
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-control", "no-cache, no-store, must-revalidate");
        response.setHeader("Expires", "01 Apr 1995 01:10:10 GMT");
    }

    if(contentType!=null)
    {
        response.setContentType(contentType);
    }

    if(fileName != null)
    {
        fileName = fileName.substring(fileName.lastIndexOf('\\')+1);
        fileName = fileName.substring(fileName.lastIndexOf('/')+1);

        StringBuffer contentDisposition = new StringBuffer();

        if(openDirectly==null || openDirectly.equalsIgnoreCase("false"))
        {
            contentDisposition.append("attachment;");
        }

        contentDisposition.append("filename=\"");
        contentDisposition.append(fileName);
        contentDisposition.append("\"");

        response.setHeader ("Content-Disposition", contentDisposition.toString());
    }

    byte[] bytes = (byte[])request.getAttribute("fileupload_bytes");
    if (bytes != null)
    {
        response.getOutputStream().write(bytes);
    }
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
